package game;
import java.awt.Color;

import gui.dialogs.GameDialog;
import gui.game.GUI;


public class GameLogic extends Thread{
	public static GameLogic INSTANCE=null;
	public boolean startGL=false;
	private GUI gui;
	private GameBoard gb;
	private PlayerDTO pl;
	//the path to the correct size of the images
	private String folderName="";
	
	public GameLogic(){
		GameLogic.INSTANCE=this;
		gui = GUI.getInstance();
		gb = GameBoard.getInstance();
		pl = PlayerDTO.getInstance();
		
//		pl.setPlayers(	new PlayerDAO(new Color ( 0,100,0),"dennis",PlayerDAO.FIGURE_CAR), 
//						new PlayerDAO(new Color ( 255,0,0),"otto",PlayerDAO.FIGURE_DOG));
		
		this.start();
	}
	
	public void run(){
		
			while (true){
					if (startGL==true){
						try {
							movePlayer();
						} catch (Exception e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						checkPlayersGameField();
						this.startGL=false;
					}
				try {
					sleep(1000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		
	}
	public static GameLogic getInstance(){
		if (GameLogic.INSTANCE==null){
			new GameLogic();
		}
		return GameLogic.INSTANCE;
	}
	
	// move current Player
	private void movePlayer () throws Exception{
		while(pl.getCurrentPlayer().getDices()>0){
			int lastField = gb.getGameBoard().size()-1;
			if (pl.getCurrentPlayer().getField() == lastField){
				pl.getCurrentPlayer().setField(0);
				pl.getCurrentPlayer().addMoney(2000000);
				gui.repaintPlayersInfo();
			}else{
				pl.getCurrentPlayer().setField(pl.getCurrentPlayer().getField()+1);
			}
			pl.getCurrentPlayer().setDices(pl.getCurrentPlayer().getDices()-1);
			gui.paintPlayerPosition();
			try {
				Thread.sleep(500);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	/**
	 * <p><P>
	 * <b>nextPlayer</b><p>
	 * {@value public void} <b>nextPlayer()<b><p>
	 * Players turn is finished and set the next player as the current player
	 */
	public void nextPlayer(){
		if (pl.getCurrentPlayer()==pl.getPlayer1()){
			pl.setCurrentPlayer(pl.getPlayer2());
		}else{
			pl.setCurrentPlayer(pl.getPlayer1());
		}
		GameField currentField = null;
		try {
			currentField = gb.getGameBoard().get(pl.getCurrentPlayer().getField());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		checkPlayersGameField();
		gui.dicesEnable();
	}
	
	/**
	 * <p><P>
	 * <b>startGL</b><p>
	 * {@value public void} <b>startGL()<b><p>
	 * will start the gamelogic in the next cyclus to calculate
	 */
	public void startGL(){
		this.startGL=true;
	}
	/**
	 * <p><P>
	 * <b>checkPlayersGameField</b><p>
	 * {@value public void} <b>checkPlayersGameField()<b><p>
	 * checks where the player is after he has moved and starts the consequences
	 */
	private void checkPlayersGameField(){
		checkForBuyableGF();
		
	}
	private void checkForBuyableGF(){
		try {
			GameField gf = gb.getGameBoard().get(pl.getCurrentPlayer().getField());
			if (gf.getOwner()==null && 
					(gf.getType()==GameField.AIRPORT || 
					gf.getType()==GameField.NORMAL ||
					gf.getType()==GameField.COMMUNICATION)){
						gui.enableBuyButton();
			} else {
				gui.disableBuyButton();
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public String getFolderName() {
		return folderName;
	}

	public void setFolderName(String folderName) {
		this.folderName = folderName;
	}

	public boolean buyStreet(GameField gf) {
		boolean answer = GameDialog.getDialog(GUI.getInstance(), gf, GameDialog.BUY);
		if (answer){
			pl.getCurrentPlayer().subMoney(gf.getCost());
			gui.repaintPlayersInfo();
		}
		return answer;
	}

}
